﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.UI;
using SBACN.WebSite.Cache;
using SBACN.WebSite.Code;
using SBACN.WebSite.Common;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.En
{
	public partial class Index : BasePage
	{
		public string ABoutSBA
		{
			get
			{
				if(ViewState["ABoutSBA"] == null)
				{
					return string.Empty;
				}
				return ViewState["ABoutSBA"].ToString();
			}
			set
			{
				ViewState["ABoutSBA"] = value;

			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				InitCtrl();
			}
		}

		private void InitCtrl()
		{
			#region 图片新闻
			TB_SystemConfig config = SystemConfigCache.Instance.List.Where(p => p.Type == "00090005" && p.Code == p.Type).FirstOrDefault();
			int picNewsCount = 0;
			if (config == null || !int.TryParse(config.Value, out picNewsCount))
			{
				picNewsCount = 5;
			}
			Expression<Func<TB_News, bool>> f = (p => p.IsImageNews && p.IsEn && p.MainImageUrl.Length > 0);
			Expression<Func<TB_News, DateTime>> o = (p => p.CreateDate);
			int newsCount = 0;
			var newsList = DAL.DALHelper.Instance.GetList(f, o, false, picNewsCount, 1, out newsCount);

			pic_container.Visible = newsList.Count > 0;

			for (int i = 1; i <= newsList.Count; i++)
			{
				numCtrl.InnerHtml += "<a>" + i + "</a>";
			}

			this.rpt_PicNews.DataSource = newsList;
			this.rpt_PicNews.DataBind();
			#endregion

			#region 关于公会

			TB_News news = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_News>(p => p.IsEn && p.Type == "00010001");
			if(news != null)
			{
				if(string.IsNullOrWhiteSpace(news.Summary) )
				{
					System.Web.UI.HtmlControls.HtmlGenericControl ctrl = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
					ctrl.InnerHtml = news.Content;
					this.ABoutSBA = ctrl.InnerText;
				}
				else
				{
					this.ABoutSBA = news.Summary;
				}
			}

			#endregion

			#region 要闻导读
			f = (p => p.Type == "00020001" && p.IsEn);
			newsList = DAL.DALHelper.Instance.GetList(f, o, false, 3, 1, out newsCount);
			this.rpt_impNews.DataSource = newsList;
			this.rpt_impNews.DataBind();
			#endregion

			#region 专委会

			Expression<Func<TB_News, bool>> f_committee = (p => p.IsEn && p.Type.StartsWith("000300") && p.Type.EndsWith("0002"));
			Expression<Func<TB_News, string>> o_committee = (p => p.Type);
			IList<TB_News> list = DAL.DALHelper.Instance.GetList(f_committee, o_committee, true);
			this.rpt_Committee.DataSource = list;
			this.rpt_Committee.DataBind();
			topboxr.Visible = list.Count > 0;
			#endregion

			#region 子系统外链
			//银团
			TB_SystemMap map = SystemMapCache.Instance["00050004"];
			if (map != null)
			{
				linkYT.HRef = map.Status ? (string.IsNullOrWhiteSpace(map.EURL) ? "javascript:void(0);" : map.EURL) : "javascript:void(0);";
				linkYT.Target = map.Status && !string.IsNullOrWhiteSpace(map.EURL) ? "_blank" : "_self";
			}
			//数据库
			map = SystemMapCache.Instance["00050005"];
			if (map != null)
			{
				linkZEF.HRef = map.Status ? (string.IsNullOrWhiteSpace(map.EURL) ? "javascript:void(0);" : map.EURL) : "javascript:void(0);";
				linkZEF.Target = map.Status && !string.IsNullOrWhiteSpace(map.EURL) ? "_blank" : "_self";
			}
			//赴美
			map = SystemMapCache.Instance["00050006"];
			if (map != null)
			{
				linkAM.HRef = map.Status ? (string.IsNullOrWhiteSpace(map.EURL) ? "javascript:void(0);" : map.EURL) : "javascript:void(0);";
				linkAM.Target = map.Status && !string.IsNullOrWhiteSpace(map.EURL) ? "_blank" : "_self";
			}
			//小微
			map = SystemMapCache.Instance["00070002"];
			if (map != null)
			{
				linkXW.HRef = map.Status ? (string.IsNullOrWhiteSpace(map.EURL) ? "javascript:void(0);" : map.EURL) : "javascript:void(0);";
				linkXW.Target = map.Status && !string.IsNullOrWhiteSpace(map.EURL) ? "_blank" : "_self";
			}
			//微博
			map = SystemMapCache.Instance["00090004"];
			if (map != null)
			{
				// 当不启用跳转到指定提示页面
				linkSmallBlog.HRef = map.Status ? (string.IsNullOrWhiteSpace(map.EURL) ? "~/EnMiniBlogMessage.aspx" : map.EURL) : "~/EnMiniBlogMessage.aspx";
				//linkSmallBlog.Target = map.Status && !string.IsNullOrWhiteSpace(map.EURL) ? "_blank" : "_blank";
				linkSmallBlog.Target = "_blank";
				linkSmallBlog.InnerHtml = "<img src=\"/enimages/img_miniblog" + (map.Status ? "1" : "0") + ".jpg\" />";
			}
			#endregion
		}
	}
}